﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class HoaDonMuaHang : Form
    {
        List<Business.HoaDonMua> danhSachHDMua;        
        List<Business.HangHoa> danhSachHangHoa;
        //List<Business.DonDatNCC> danhSachDonDatHang;

        Business.HoaDonMua HoaDonMua;
        Business.HangHoa hangHoa;
        
        public HoaDonMuaHang()
        {
            InitializeComponent();
        }

        private void HoaDonMuaHang_Load(object sender, EventArgs e)
        {
            LoadDanhSachHang();
            
            LoadDanhsachHoaDon(true);

            dtpickerDenNgay.CustomFormat = "dd/MM/yyyy";
            //dtpickerDenNgaydshd.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayTaoHD.CustomFormat = "dd/MM/yyyy";
            dtpickerTuNgay.CustomFormat = "dd/MM/yyyy";
            //dtpickerTuNgaydshd.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayTaoHD.CustomFormat = "dd/MM/yyyy";
                      
            

            HoaDonMua = new Business.HoaDonMua();
            
            txtThueSuat.Text = HoaDonMua.ThueSuatHienThi.ToString();

            txtMaHDlaphd2.Text = "";
            txtDonDatlaphd.Text = "";


            XoaTrangChiTietHDMua();
            gridviewCTHD.DataSource = null;
            txtCongTienHang.Text="";
            txtTongTien2.Text = "";

            dtpickerNgayTaoHD.Enabled = true;
            cbxTenHang.Enabled = true;
            txtSoLuongNhap.Enabled = true;
            txtGiaNhap.Enabled = true;
            txtTiLeCK.Enabled = true;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDanhsachHoaDon(false);
        }


       

        private void LoadDanhSachHang()
        {
            danhSachHangHoa = Business.HangHoa.LayDanhSach();

            cbxTenHang.DataSource = danhSachHangHoa;
            cbxTenHang.DisplayMember = "TenHang";
            cbxTenHang.ValueMember = "MaHangHienThi";

            cbxTenHang.SelectedItem = null;
        }
       
        private void LoadDanhsachHoaDon(bool lanDau)
        {
            if (lanDau)
            {
                danhSachHDMua = Business.HoaDonMua.LayDanhSach();
            }
            else
            {
                if (rbtnNgaydshd.Checked && dtpickerTuNgay.Value != null)
                    danhSachHDMua = Business.HoaDonMua.LayDanhSach_NgayMua(dtpickerTuNgay.Value, dtpickerDenNgay.Value);
                else if (rbtnMaDonDat.Checked && txtMaDonDat.Text.Contains("DDNCC"))
                    danhSachHDMua = Business.HoaDonMua.LayDanhSach_MaDonDat(txtMaDonDat.Text);
                else if (rbtnMaHDdshd.Checked && txtMaHD.Text.Contains("HDM"))
                    danhSachHDMua = Business.HoaDonMua.LayDanhSach_MaHoaDonMua(txtMaHD.Text);
                else
                    danhSachHDMua = Business.HoaDonMua.LayDanhSach();
            }

            gridviewDSHD.DataSource = danhSachHDMua;
            gridviewDSHD.Columns["MaHDNhap"].Visible = false;
            gridviewDSHD.Columns["ThueSuat"].Visible = false;
            gridviewDSHD.Columns["MaDonDatNCC"].Visible = false;
            
            gridviewDSHD.Columns["NgayMua"].DefaultCellStyle.Format = "dd/MM/yyyy";
            gridviewDSHD.Columns["TongGT"].DefaultCellStyle.Format = "##,###,###";
            gridviewDSHD.Columns["TienSauThue"].DefaultCellStyle.Format = "##,###,###";

            gridviewDSHD.Columns["MaHDNhapHienThi"].HeaderText = "Mã hóa đơn";
            
            gridviewDSHD.Columns["MaDonDatNCCHienThi"].HeaderText = "Đơn đặt hàng";
            gridviewDSHD.Columns["NgayMua"].HeaderText = "Ngày mua";
            gridviewDSHD.Columns["TongGT"].HeaderText = "Tiền trước thuế";
            gridviewDSHD.Columns["TienSauThue"].HeaderText = "Tiền sau thuế";
            gridviewDSHD.Columns["ThueSuatHienThi"].HeaderText = "Thuế suất (%)";
        }

        private void btnThemHD_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = CTHDtabPage;
            dtpickerNgayTaoHD.CustomFormat = "dd/MM/yyyy";
            HoaDonMua = new Business.HoaDonMua();
            
            txtThueSuat.Text = HoaDonMua.ThueSuatHienThi.ToString();

            txtMaHDlaphd2.Text = "";
            txtDonDatlaphd.Text = "";
            txtDonDatlaphd.ReadOnly = false;
            txtDonDatlaphd.Focus();
            
            txtMaHDlaphd2.Enabled = true;
            

            XoaTrangChiTietHDMua();

            gridviewCTHD.DataSource = null;
            txtCongTienHang.Text = "";
            txtTongTien2.Text = "";

            dtpickerNgayTaoHD.Enabled = true;
            dtpickerNgayTaoHD.Value = DateTime.Now;

            btnLuu2.Enabled = true;
            btnLapPhieuNK.Enabled = true;

            cbxTenHang.Enabled = true;
            txtSoLuongNhap.ReadOnly = false;
            txtGiaNhap.ReadOnly = false;
            
            txtTiLeCK.ReadOnly = false;
            btnThemCTHD.Enabled = true;
            btnLuuCTHD.Enabled = true;
            btnXoaCTHD.Enabled = true;


        }

        private void btnXemHD_Click(object sender, EventArgs e)
        {
            if (danhSachHDMua != null && danhSachHDMua.Count > 0)
            {
                tabControl1.SelectedTab = this.CTHDtabPage;
                if (gridviewDSHD.SelectedRows.Count == 0)
                {
                    int maHDMua = danhSachHDMua[0].MaHDNhap;
                    ChoPhepChinhSua(maHDMua);
                    LoadThongTinHoaDonMua(maHDMua);
                }
                else
                {
                    int maHDMua = danhSachHDMua[gridviewDSHD.SelectedRows[0].Index].MaHDNhap;
                    ChoPhepChinhSua(maHDMua);
                    LoadThongTinHoaDonMua(maHDMua);
                }
            }
        }

        private void ChoPhepChinhSua(int maHDMua)
        {
            if (!Business.HoaDonMua.ChoPhepXoaChinhSua(maHDMua))
            {
                btnThemCTHD.Enabled = false;
                btnLuuCTHD.Enabled = false;
                btnXoaCTHD.Enabled = false;

                btnLuu2.Enabled = false;
                btnLapPhieuNK.Enabled = false;
                dtpickerNgayTaoHD.Enabled = false;
                txtDonDatlaphd.ReadOnly = true;
                cbxTenHang.Enabled = false;
                txtSoLuongNhap.ReadOnly = true;
                txtGiaNhap.ReadOnly = true;
                txtTiLeCK.ReadOnly = true;
                txtThueSuat.ReadOnly = true;
            }
            else
            {
                btnThemCTHD.Enabled = true;
                btnLuuCTHD.Enabled = true;
                btnXoaCTHD.Enabled = true;

                btnLuu2.Enabled = true;
                btnLapPhieuNK.Enabled = true;
                dtpickerNgayTaoHD.Enabled = true;
                txtDonDatlaphd.ReadOnly = false;
                cbxTenHang.Enabled = true;
                txtSoLuongNhap.ReadOnly = false;
                txtGiaNhap.ReadOnly = false;
                txtTiLeCK.ReadOnly = false;
                txtThueSuat.ReadOnly = false;
            }
        }

        private void LoadThongTinHoaDonMua(int maHDMua)
        {
            HoaDonMua = Business.HoaDonMua.LayHoaDonMua(maHDMua);
            dtpickerNgayTaoHD.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayTaoHD.Text = HoaDonMua.NgayMua.ToString();
            txtMaHDlaphd2.Text = HoaDonMua.MaHDNhapHienThi;

            txtDonDatlaphd.Text = HoaDonMua.MaDonDatNCCHienThi.ToString();
            txtThueSuat.Text = HoaDonMua.ThueSuatHienThi.ToString();
            

            LoadDanhSachChiTietHDMua();
        }

        public void LoadDanhSachChiTietHDMua()
        {
            BindingSource source = new BindingSource();
            source.DataSource = HoaDonMua.DanhSachChiTietHDMua;

            gridviewCTHD.DataSource = source;
            gridviewCTHD.RefreshEdit();
            gridviewCTHD.Refresh();

            gridviewCTHD.Columns["MaHDNhap"].Visible = false;
            gridviewCTHD.Columns["MaHang"].Visible = false;
            gridviewCTHD.Columns["MaCTHDN"].Visible = false;
            gridviewCTHD.Columns["ChietKhau"].Visible = false;

            gridviewCTHD.Columns["MaCTHDNHienThi"].HeaderText = "Mã chi tiết hhóa đơn";
            gridviewCTHD.Columns["MaHangHienThi"].HeaderText = "Mã hàng";
            gridviewCTHD.Columns["TenHang"].HeaderText = "Tên hàng";
            gridviewCTHD.Columns["SoLuong"].HeaderText = "Số lượng";
            gridviewCTHD.Columns["GiaNhap"].HeaderText = "Giá nhập";
            gridviewCTHD.Columns["ChietKhauHienThi"].HeaderText = "Chiết khấu (%)";

            gridviewCTHD.Columns["GiaNhap"].DefaultCellStyle.Format = "##,###,###";

            HoaDonMua.TinhTongTienSauThue(double.Parse(txtThueSuat.Text)).ToString();
            txtCongTienHang.Text = HoaDonMua.TongGT.ToString();
            txtTongTien2.Text = HoaDonMua.TienSauThue.ToString();
        }

        private void cbxTenHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxTenHang.SelectedItem != null && HoaDonMua != null)
            {
                hangHoa = danhSachHangHoa[cbxTenHang.SelectedIndex];
                txtMaHang.Text=hangHoa.MaHangHienThi.ToString();
               
            }
        }

        bool dangThemMoi = true;

        private void gridviewCTHD_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ChiTietHDMua chiTietHDMua = HoaDonMua.DanhSachChiTietHDMua[e.RowIndex];
            cbxTenHang.SelectedValue = "HH" + chiTietHDMua.MaHang;
            txtGiaNhap.Text = chiTietHDMua.GiaNhap.ToString();
            txtSoLuongNhap.Text = chiTietHDMua.SoLuong.ToString();
            txtTiLeCK.Text = chiTietHDMua.ChietKhauHienThi.ToString();
            dangThemMoi = false;
        }


        private void XoaTrangChiTietHDMua()
        {
            cbxTenHang.SelectedItem = null;
            txtMaHang.Text = "";
            txtSoLuongNhap.Text = "";
            txtTiLeCK.Text = "0";
            txtGiaNhap.Text = "";

            dangThemMoi = true;
        }

        private void btnThemCTHD_Click(object sender, EventArgs e)
        {
            XoaTrangChiTietHDMua();
        }

        private void btnLuuCTHD_Click(object sender, EventArgs e)
        {
            int maHDMua = HoaDonMua.MaHDNhap;
            int maHang = Int16.Parse(txtMaHang.Text.Substring(2));
            string tenHang = ((HangHoa)cbxTenHang.SelectedItem).TenHang;
            int soLuong = Int16.Parse(txtSoLuongNhap.Text);
            double giaNhap = double.Parse(txtGiaNhap.Text);
            double chietKhau = double.Parse(txtTiLeCK.Text) / 100;

            ChiTietHDMua chiTietHDMua = new ChiTietHDMua(maHDMua, maHang, tenHang, giaNhap, soLuong, chietKhau);

            if (dangThemMoi == true)
            {
                HoaDonMua.ThemMoiChiTietHDMua(maHDMua, maHang, tenHang, giaNhap, soLuong, chietKhau);
            }
            else
            {
                if (gridviewCTHD.SelectedRows.Count > 0)
                {
                    int index = gridviewCTHD.SelectedRows[0].Index;
                    HoaDonMua.CapNhatChiTietDonDat(index, maHang, tenHang, giaNhap, soLuong, chietKhau);
                }
            }
            LoadDanhSachChiTietHDMua();
            XoaTrangChiTietHDMua();
        }

        private void btnXoaCTHD_Click(object sender, EventArgs e)
        {
            int index = gridviewCTHD.SelectedRows[0].Index;
            HoaDonMua.XoaChiTietHDMua(index);
            LoadDanhSachChiTietHDMua();
        }

        private void btnLuu2_Click(object sender, EventArgs e)
        {
            if (HoaDonMua.MaHDNhap == 0)
            {
                CapNhatThongTinChung();
                int maHDBan = Business.HoaDonMua.ThemMoi(HoaDonMua);

                LoadThongTinHoaDonMua(maHDBan);
                MessageBox.Show("Thêm mới hóa đơn bán thành công");
            }
            else
            {
                CapNhatThongTinChung();
                Business.HoaDonMua.CapNhat(HoaDonMua);

                LoadThongTinHoaDonMua(HoaDonMua.MaHDNhap);
                MessageBox.Show("Cập nhật hóa đơn bán thành công");
            }
            XoaTrangChiTietHDMua();
        }

        private void CapNhatThongTinChung()
        {
            //sHoaDonMua.MaNV = Int16.Parse(txtMaNVcthd.Text.Substring(2));
            HoaDonMua.MaDonDatNCC =  Int16.Parse(txtDonDatlaphd.Text.Substring(5));
            HoaDonMua.NgayMua =  dtpickerNgayTaoHD.Value;
            HoaDonMua.TinhTongTienSauThue(double.Parse(txtThueSuat.Text));
        }

        private void btnLapPhieuNK_Click(object sender, EventArgs e)
        {
            if (HoaDonMua.MaHDNhap == 0)
            {
                MessageBox.Show("Phải lưu hóa đơn trước khi lập phiếu nhập kho");
            }
            else
            {
                PhieuNhapKho phieuNhapKho = new PhieuNhapKho();
                phieuNhapKho.LayTheoHDM(HoaDonMua.MaHDNhap);
                phieuNhapKho.ShowDialog();
            }
        }

       

       

       
       
    }
}
